package com.hsd.znsh.controller.pc;

import com.hsd.znsh.entity.SensorData;
import com.hsd.znsh.entity.SensorGroup;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.service.SensorDataService;
import com.hsd.znsh.service.SensorGroupService;
import com.hsd.znsh.service.SensorService;
import com.hsd.znsh.service.UserService;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-27 10:18:15
 */
@RestController
@RequestMapping("/pc/sensor")
public class PCSensorController {

    @Autowired
    private UserService userService;

    @Autowired
    private SensorGroupService sensorGroupService;

    @GetMapping("/getSensorGroupList")
    public ResultVO getSensorList(){
        List<SensorGroup> list=sensorGroupService.getSensorGroupList();
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getUserSensorGroupList")
    public ResultVO getUserSensorGroupList(){
        List<SensorGroup> list;
        if(SecurityUtil.hasRole("ROLE_PC_MANAGER")){
            list=sensorGroupService.getSensorGroupList();
        }else{
            list=sensorGroupService.getUserSensorGroupList(SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getECList")
    public ResultVO getECList(){
        List<SensorGroup> list=sensorGroupService.getECList();
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getUserECList")
    public ResultVO getUserECList(){
        List<SensorGroup> list;
        if(SecurityUtil.hasRole("ROLE_PC_MANAGER")){
            list=sensorGroupService.getECList();
        }else{
            list=sensorGroupService.getUserECList(SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/{property}")
    public ResultVO findSensorData(Integer id,@PathVariable("property") String property){
        List<Map<String,Object>> list=sensorGroupService.findSensorData(id,property);
        return ResultVOUtil.success(list);
    }


    @GetMapping("/history/{property}/{bh}")
    public ResultVO findHistorySensorData(Integer id,Integer totalTime,
                                          @PathVariable("property") String property,
                                          @PathVariable("bh") Integer bh){
        List<SensorData> list=sensorGroupService.findHistorySensorData(id,totalTime,property,bh);
        return ResultVOUtil.success(list);
    }

}
